package com.kaili.basic.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseStoreHouseAddDto;
import com.kaili.basic.pojo.dto.BaseStoreHouseEditDto;
import com.kaili.basic.pojo.dto.BaseStoreHouseQueryDto;
import com.kaili.basic.pojo.dto.BaseStoreLocationEditDto;
import com.kaili.basic.pojo.entity.BaseDict;
import com.kaili.basic.pojo.entity.BaseStoreHouseEntity;
import com.kaili.basic.pojo.entity.BaseStoreLocationEntity;
import com.kaili.basic.pojo.vo.BaseStoreHouseLikeVO;
import com.kaili.basic.pojo.vo.BaseStoreHouseVO;
import com.kaili.basic.service.BaseStoreHouseService;
import com.kaili.basic.service.BaseStoreLocationService;
import com.kaili.basic.service.IBaseDictService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.*;

/**
 * @author lsw
 * @Type BaseStoreHouse
 * @Desc 杭州凯立通信有限公司Controller
 * @Date 2019年03月07日
 */
@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Transactional
@Api(tags = "仓库管理", description = "仓库管理")
public class BaseStoreHouseController {
    @Autowired
    private BaseStoreHouseService baseStoreHouseService;

    @Autowired
    private BaseStoreLocationService baseStoreLocationService;

    @Autowired
    private IBaseDictService baseDictService;

    @GetMapping("/storeHouse/code")
    @ApiOperation(value = "查询仓库编号")
    public Long queryStoreCoude(){
        return baseStoreHouseService.queryStoreCoude();
    }


    @GetMapping("/storeHouse")
    @ApiOperation(value = "分页查询仓库")
    public Page<BaseStoreHouseEntity> queryPageList(@ModelAttribute BaseStoreHouseQueryDto queryDto){
        return baseStoreHouseService.queryPageList(new Page<BaseStoreHouseEntity>(queryDto.getPage(),queryDto.getPageSize()),queryDto);
    }

    @GetMapping("/storeHouse/export")
    @ApiOperation(value = "仓库导出")
    public void export(HttpServletResponse response, @ModelAttribute BaseStoreHouseQueryDto queryDto) throws RestException {
        List<BaseStoreHouseVO> list = baseStoreHouseService.export(queryDto);
        ExcelUtil.exportExcel(list,"仓库信息","sheet1", BaseStoreHouseVO.class, UUID.randomUUID().toString()+".xls",response);

    }

    @GetMapping("/storeHouse/all")
    @ApiOperation(value = "查询所有仓库")
    public List<BaseStoreHouseLikeVO> queryAllStoreHouse(){
        return baseStoreHouseService.queryAllStoreHouse();
    }

    @PostMapping("/storeHouse")
    @ApiOperation(value = "新增仓库")
    public RestStatus addStoreHouse(@RequestBody BaseStoreHouseAddDto addDto){
        BaseStoreHouseEntity entity=new BaseStoreHouseEntity();
        entity.setCreateTime(new Date());
        entity.setCreateBy(1);
        BeanUtils.copyProperties(addDto,entity);
        baseStoreHouseService.insert(entity);
        //自动新增默认库位
        List<BaseDict> typeList = baseDictService.getDictByParentId(210);
        List<BaseStoreLocationEntity>  locationList=new ArrayList<>();
        for (BaseDict dict:typeList) {
            BaseStoreLocationEntity location=new BaseStoreLocationEntity();
            location.setStoreId(entity.getId());
            location.setStoreLocationCode(String.valueOf(baseStoreLocationService.queryStoreLocationCoude()));
            location.setStoreLocationName("默认"+dict.getDictName());
            location.setStoreLocationType(String.valueOf(dict.getDictId()));
            location.setDefaultLocation(0);
            location.setRemark("系统自动创建");
            location.setCreateBy(LoginDetailUtils.getCurrentEmployeeId());
            location.setCreateTime(new Date());
            locationList.add(location);
        }
        baseStoreLocationService.insertBatch(locationList);
        return CommonStatus.ADD_OK;
    }

    @PutMapping("/storeHouse")
    @ApiOperation(value = "修改仓库")
    public RestStatus updateStoreHouse(@RequestBody BaseStoreHouseEditDto addDto){
        BaseStoreHouseEntity entity=new BaseStoreHouseEntity();
        entity.setModifyTime(new Date());
        entity.setModifyBy(1);
        BeanUtils.copyProperties(addDto,entity);
        baseStoreHouseService.updateById(entity);
        return CommonStatus.UPDATE_OK;
    }

    @PutMapping("/storeHouse/status")
    @ApiOperation(value = "修改仓库状态")
    public RestStatus updateStoreStatus(@RequestBody BaseStoreLocationEditDto dto){
        BaseStoreHouseEntity entity=new BaseStoreHouseEntity();
        entity.setId(dto.getId());
        entity.setStatus(dto.getStatus());
        baseStoreHouseService.updateById(entity);
        return CommonStatus.UPDATE_OK;
    }

    @PutMapping("/storeHouse/default")
    @ApiOperation(value = "设置默认仓库")
    public RestStatus updateStoreDefault(@RequestBody BaseStoreLocationEditDto dto){
        if(dto.getStatus()==0){
            baseStoreHouseService.updateDefaultStore(new HashMap<String,Object>(){{put("defaultStore",1);}});
        }
        Map<String,Object> map=new HashMap<>();
        map.put("id",dto.getId());
        map.put("defaultStore",dto.getStatus());
        baseStoreHouseService.updateDefaultStore(map);
        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/storeHouse/{id}")
    @ApiOperation(value = "删除仓库")
    public RestStatus delStoreHouse(@PathVariable("id") Integer id){
        baseStoreHouseService.deleteById(id);
        return CommonStatus.DELETE_OK;
    }
}
